package com.example.principle.controller;

import com.example.principle.model.Server;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/server")
public class ServerController {
    @Autowired
    private ApplicationContext context;

    @Autowired
    @Resource(name = "getServer")
    private Server server;

    @Autowired
    @Resource(name = "getSingleton")
    private Server singletonServer;

    @Autowired
    @Resource(name = "getPrototype")
    private Server prototypeServer;

    @Autowired
    @Resource(name = "getRequest")
    private Server requestServer;

    @Autowired
    @Resource(name = "getSession")
    private Server sessionServer;

    @Autowired
    @Resource(name = "getApplication")
    private Server applicationServer;

    @RequestMapping("/show")
    public String show(){
        Server server1 = context.getBean("getServer", Server.class);

        Server server2 = server;

        return server1.toString() + ", <br/>" + server2.toString();
    }

    @RequestMapping("/singleton")
    public String singleton(){
        Server server1 = context.getBean("getSingleton", Server.class);

        Server server2 = singletonServer;

        return server1.toString() + ", <br/>" + server2.toString();
    }

    @RequestMapping("/prototype")
    public String prototype(){
        Server server1 = context.getBean("getPrototype", Server.class);

        Server server2 = prototypeServer;

        return server1.toString() + ", <br/>" + server2.toString();
    }

    @RequestMapping("/request")
    public String request(){
        Server server1 = context.getBean("getRequest", Server.class);

        Server server2 = requestServer;

        return server1.toString() + ", <br/>" + server2.toString();
    }

    @RequestMapping("/session")
    public String session(){
        Server server1 = context.getBean("getSession", Server.class);

        Server server2 = sessionServer;

        return server1.toString() + ", <br/>" + server2.toString();
    }

    @RequestMapping("/application")
    public String application(){
        Server server1 = context.getBean("getApplication", Server.class);

        Server server2 = applicationServer;

        return server1.toString() + ", <br/>" + server2.toString();
    }
}
